<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/2 0002
 * Time: 下午 13:31
 */

namespace Home\Controller;
use Think\Controller;
/**
 *元器件系列控制器CpxlController
 */

class CpxlController extends Controller{
    protected $model = null;
    public function __construct(){
        parent::__construct();
        $this->model = D('b_p_cpxl');
    }

    public function cpxllist(){
        $name = I('get.name');
        $flno = I('get.flno');
        $gysno = I('get.gysno');

        /****************从缓存里取出数据,如果没有,从数据库里取**************/
        $urlencode_name = urlencode($name);
        $key = $urlencode_name.'_xl_cpxllist';
        $redis = linkredis();
        $data = $redis->get($key);
        $filter = [];   //筛选数据

        if($data){
            $_data = unserialize($data);
            $cpxlnos = $_data['cpxlnos'];
            $filter = $_data['filter'];
        }else{
            //全文检索返回系列的ID
            $cpxlnos = $this->model->sphinx($name);
            if($cpxlnos){
                //获取筛选数据并存入缓存
                $filter = $this->model->get_cpxllist_filter($cpxlnos);
                $info = array('filter'=>$filter,'cpxlnos'=>$cpxlnos);
                $redis->set($key,serialize($info));
                $redis->expire($key,3600*48);
            }
        }

        /***********************获取产品系列信息*************************/
        $cpxl_info = [];
        //判断如果有筛选值提交,就调用sphinx类进行过滤返回系列ID,通过过滤后的ID查出数据
        if($flno || $gysno) {
            $filter_cpxlnos = $this->model->sphinxFilter($name);
            //dump($filter_cpxlnos);exit;
            $count = count($filter_cpxlnos);
            if ($count > 0) {
                $cpxl_info = $this->model->search($filter_cpxlnos);
            }
            //dump($cpxl_info);exit;
            $this->assign(array(
                'count'   => $count,
                'tiaoshu' => $cpxl_info['tiaoshu'],
                'info'    => $cpxl_info['info'],
                'cpxlnos' => implode(',', $filter_cpxlnos),
            ));
        }else{
            $count = count($cpxlnos);
            if($count > 0){
                $cpxl_info = $this->model->search($cpxlnos);
            }
            $this->assign(array(
                'count'   => $count,
                'tiaoshu' => $cpxl_info['tiaoshu'],
                'info'    => $cpxl_info['info'],
                'cpxlnos' => implode(',', $cpxlnos),
            ));
        }

        $this->assign(array(
            'title'      => '系列列表',
            'name'       => $name,
            'filter'     => $filter,
            'page_title' => '系列',
        ));
        $this->display();
    }
    
    //点击加载获取更多的系列下
    public function ajax_getcpxloffset(){
        $cpxlnos = I('post.cpxlnos');
        $data = $this->model->search($cpxlnos);
        $this->ajaxReturn( array(
            'info' => $data['info'],
            'tiaoshu' => $data['tiaoshu'],
        ));
    }

    //系列介绍
    public function cpxl_present(){
        $cpxlno = I('get.cpxlno');
        $cpxlmc = $this->model->field('xpxlmc')->find($cpxlno);
        /***************************取出该系列的扩展信息************************/
        $xl_expend = D('Admin/xl_expend');
        $xl_info= $xl_expend->search_one($cpxlno);
        //dump($xl_info);exit;
        /****************************取出该系列下的三条物资信息*********************/
        $data = $this->model->getwxbminfo($cpxlno);
        //dump($data);exit;

        $this->assign(array(
            'title' => $cpxlmc['xpxlmc'] .'的介绍',  //视图标题
            'cpxlmc' => $cpxlmc['xpxlmc'],  //系列名称
            'cpxlno' => $cpxlno,  //系列编码
            'data' => $data['info'],  //系列下的三条随机物资编码信息
            'wzno_count' => $data['count'] , //系列下的所有物资数量
            'xl_info' => $xl_info  //系列扩展信息
        ));
        $this->display();
    }

    //系列单品列表
    public function cpxlwzbm(){
        $cpxlno = I('get.cpxlno');
        $cpxlmc = I('get.cpxlmc');

        $b_p_cpxlwzbm = D('b_p_cpxlwzbm');
        $wzbm_count = $b_p_cpxlwzbm->where("cpxlno={$cpxlno}")->count();
        //dump($wzbm_count);exit;
        $list = [];
        if($wzbm_count > 1){
            //从缓存里取出该系列的筛选数据
            $redis = linkredis();
            $filter_info = $redis->get($cpxlno.'_xl_wzbmfilter');
            if($filter_info){
                $list = unserialize($filter_info);
            }else{
                $list['sx'] = $this->model->getcpxlwzbm_sx_filter($cpxlno);
            }
        }
        //dump($list);exit;
        if($list['xh']){  //型号状态
            $xh_type = true;
        }else{
            $xh_type = false;
        }
        if(!$list){
            $xh_type = true;
        }
        $b_c_wzbm = D('b_c_wzbm');
        $data = $b_c_wzbm->getcatwzbm($cpxlno,'cpxl');
        //dump($data);exit;
        $this->assign(array(
            'count' => $wzbm_count,
            'info' => $data['info'],
            'tiaoshu' => $data['tiaoshu']
        ));
        $this->assign(array(
            'cpxlno' => $cpxlno,
            'title' => $cpxlmc.'系列下的单品',  //选项卡标题
            'cpxlmc' => $cpxlmc,
            'filter' => $list, //筛选数据
            'xh_type' => $xh_type, //型号状态
        ));
        $this->display();
    }

    //系列单品页面加载完毕ajax获取该系列单品的型号信息
    public function ajax_xh_filter(){
        $cpxlno = I('get.cpxlno');
        //$cpxlno = 2083;
        $sx_info = $this->model->getcpxlwzbm_xh_filter($cpxlno);
        //dump($sx_info);
        $this->ajaxReturn(array(
            'xh' => $sx_info
        ));
    }

    //点击加载获取该系列下更多的单品
    public function ajax_getwzbm(){
        $cpxlno = I('post.cpxlno');
        //从缓存里取出该系列的筛选数据
        //$list = getcpxlwzbmfilter($cpxlno);
        $b_c_wzbm = D('b_c_wzbm');
        $data = $b_c_wzbm->getcatwzbm($cpxlno,'cpxl');
        $this->ajaxReturn( array(
            'info' => $data['info'],
            'tiaoshu' => $data['tiaoshu']
        ));
    }

    public function filter_cpxlwzbm(){
        //dump(I('get.'));exit;
        $cpxlno = I('get.cpxlno');
        $cpxlmc = I('get.cpxlmc');
        $xh = I('get.xh');
        $sx = I('get.sx');
        //判断有没有条件筛选条件
        if($xh || $sx){
            //从缓存中取出筛选数据
            $redis = linkredis();
            $filter_info = $redis->get($cpxlno.'_xl_wzbmfilter');
            if($filter_info){
                $list = unserialize($filter_info);
            }else{
                $list = $this->model->getcpxlwzbmfilter($cpxlno);
            }
            //dump($list);exit;
            /**-------------------把提交的筛选条件和分类ID拼接作为redis存储的键-------------------*/
            $key = 'cpxl-'.$cpxlno.'-';
            $key .= implode('-',$xh).'-';

            foreach($sx as $k=>$v){
                $key .= implode('-',$v).'-';
            }
            //dump($key);exit;
            $wznos = $redis->get($key);
            if($wznos){
                $wznoinfo = unserialize($wznos);
            }else {
                $wznoinfo = $data = $this->model->get_tj_cpxlwzbm($cpxlno);
                if($wznoinfo){
                    //把取得的物资ID存入redis,用上面拼接的字符串做键
                    $redis = linkredis();
                    $redis->set($key,serialize($wznoinfo));
                    $redis->expire($key,3600);
                }
            }
            $data = [];
            if($wznoinfo){
                //通过物资ID取出相应条数的数据
                $b_c_wzfl = D('b_c_wzfl');
                $data = $b_c_wzfl->get_wzfl_wzbm_filter($wznoinfo);
            }

            //dump($data);exit;
            $this->assign(array(
                'cpxlno' => $cpxlno,
                'title' => $cpxlmc.'系列下的单品',  //选项卡标题
                'cpxlmc' => $cpxlmc,
                'filter' => $list, //筛选数据
            ));
            $this->assign(array(
                'count' => count($wznoinfo),  //根据条件筛选取出数据的总条数
                'info' => $data['info'],  //根据条件筛选取出物资ID取出相应条数的数据
                'key' => $key,  //当前提交的条件拼接的redis的key键
                'offset' => $data['offset']  //已取数据的条数
            ));
            $this->display();
        }else{
            $this->redirect('cpxlwzbm',array('cpxlno'=>$cpxlno,'cpxlmc'=>$cpxlmc));
        }

    }

    public function ajax_filter_getwzbm(){
        $key = I('post.key');
        $b_c_wzfl = D('b_c_wzfl');
        $redis = linkredis();
        $wznoinfo = unserialize($redis->get($key));
        $data = $b_c_wzfl->get_wzfl_wzbm_filter($wznoinfo);
        $this->ajaxReturn($data);
    }


    public function cpxlwzbmfilter(){
        $cpxlno = I('post.cpxlno');
        $cpxlmc = I('post.cpxlmc');
        $xh = I('post.xh');
        $sx = I('post.sx');
        //从缓存里取出该系列的筛选数据
        $list = getcpxlwzbmfilter($cpxlno);
        $count = count($list['wznos']);
        if($xh){
            //从缓存里取出该系列的筛选数据
            $list = getcpxlwzbmfilter($cpxlno);
            $count = count($list['wznos']);

            $b_c_wzbm = D('b_c_wzbm');
        }
    }

}